package com.yunjie.action.profile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.yunjie.action.BaseAction;
import com.yunjie.model.ComProfile;
import com.yunjie.model.tree.NodeModel;
import com.yunjie.service.comprofile.ProfileService;
/**
 * 公司档案相关action
 * @author yoyo
 *
 */
@Controller("profileAction")
@Scope("prototype")
public class ProfileAction extends BaseAction implements ModelDriven<ComProfile> {
	private static final long serialVersionUID = 7934379825159713619L;
	private ComProfile profile = new ComProfile();
	@Autowired
	private ProfileService profileService;
	private Map<String, Object> msg = new HashMap<String, Object>();
	@Override
	public ComProfile getModel() {
		// TODO Auto-generated method stub
		return this.profile;
	}
	public ComProfile getProfile() {
		return profile;
	}
	public void setProfile(ComProfile profile) {
		this.profile = profile;
	}
	public Map<String, Object> getMsg() {
		return msg;
	}
	public void setMsg(Map<String, Object> msg) {
		this.msg = msg;
	}
	/**
	 * 得到树模型数据
	 * @return
	 * @throws Exception
	 */
	public String treeModel() throws Exception {
		Set<NodeModel> nodes = profileService.getNodesByLevel(1);
		//msg.put("success", true);
		msg.put("children", nodes);
		return SUCCESS;
	}
	/**
	 * 得到子节点树模型数据
	 * @return
	 * @throws Exception
	 */
	public String subNodeModel() throws Exception {
		Set<NodeModel> nodes = profileService.getSubNodes(parentId);
		//msg.put("success", true);
		msg.put("children", nodes);
		return SUCCESS;
	}
	
	public String save() throws Exception {
		profile = profileService.save(profile, parentId);
		//msg.put("success", true);
		msg.put("profile", profile);
		return SUCCESS;
	}
	
	public String updata() throws Exception {
		profile = profileService.updata(profile, parentId);
		//msg.put("success", true);
		msg.put("profile", profile);
		return SUCCESS;
	}
	
	public String delete() throws Exception {
		profileService.delete(id);
		msg.put("success", true);
		return SUCCESS;
	}
	
	public String list() throws Exception {
		List<ComProfile> list = profileService.findAll(page, start, limit);
		//msg.put("success", true);
		msg.put("profiles", list);
		return SUCCESS;
	}
	
	public String subProfiles() throws Exception {
		List<ComProfile> list = profileService.findByParentId(parentId);
		//msg.put("success", true);
		msg.put("profiles", list);
		return SUCCESS;
	}
	
	public String profileInfo() throws Exception {
		profile = profileService.findById(id);
		//msg.put("success", true);
		msg.put("profile", profile);
		return SUCCESS;
	}
}
